package com.test.vue.service.impl;

import com.test.vue.entity.CheckUserDate;
import com.test.vue.entity.User;
import com.test.vue.entity.UserExample;
import com.test.vue.mapper.UserMapper;
import com.test.vue.service.UserService;
import com.test.vue.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    //登录
    @Override
    public ReturnResult login(String username, String password, HttpServletRequest request, HttpServletResponse response) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> list = userMapper.selectByExample(userExample);
        ReturnResult result = new ReturnResult();
        // 如果查到的结果为空,返回"检查用户名是否正确"
        if (list == null || list.isEmpty()) {
            result.setStatus(400);
            result.setMsg("请检查用户名是否正确");
            return ReturnResult.build(400,"请检查您的用户名是否正确");
        }
        //如果list不为空
        User user = list.get(0);
        //检查密码是否正确
        if (user.getPassword().equals(password)){
            //如果正确,生成一个token存在cookie中
            String token = UUID.randomUUID().toString();
            CookieUtils.addCookie(response,"LOGIN_COOKIE",token,1800);
            //将用户信息存到redis中
            user.setPassword("已加密");
            Jedis jedis = new Jedis();
            jedis.set(token, JsonUtils.objectToJson(user));
            //设置key的过期时间
            jedis.expire(token,1800);
            //这个类完全没必要加
            return ReturnResult.build(200,"登陆成功",token,user);

        }

        return ReturnResult.build(400,"密码错误,请重试");

    }

    @Override
    public CheckUserDate checkUsername(String username) {
        //校验用户名是否符合规则
       if (RegexValidateUtil.checkUsername(username) == false){
           //如果校验不合格
           System.out.println("111");
           return CheckUserDate.build(400,"用户名不符合规范");
       }
        //注册时检查用户名是否被占用
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> list = userMapper.selectByExample(userExample);
        // 如果查到的结果为空,返回"检查用户名是否正确"
        if (list == null || list.isEmpty()) {
            return CheckUserDate.build(200,"可以使用的用户名");
        }
        //否则则是被占用了
       return CheckUserDate.build(400,"未知错误,请重试") ;
    }

    //注册
    @Override
    public ReturnResult regist(User user) {
        user.setCreated(new Date());
        user.setUpdated(new Date());
        userMapper.insert(user);
        return ReturnResult.build(200,"注册成功");
    }

}
